<%@ page import="java.io.*, java.text.*, java.util.Date, java.sql.*, javax.servlet.*, javax.servlet.http.*, java.util.*" %>
<%
String user_name =request.getParameter("username");
String passwd = request.getParameter("password");

if(user_name != null && passwd != null) {

String[] events = new String[4];
SimpleDateFormat time = new SimpleDateFormat("hh:mm:ss a");
SimpleDateFormat day = new SimpleDateFormat("MM/dd/yyyy");
Connection connection = null;
Statement statement = null;
ResultSet rs = null;
ResultSet rs0 = null;
ResultSet rs1 = null;
ResultSet rs2 = null;
ResultSet rs3 = null;

String driver ="com.mysql.jdbc.Driver";
String url = "jdbc:mysql://localhost:3306/projects";
String username = "ia";
String password = "123";
events[2]= day.format(new Date());
events[3]= time.format(new Date());
try
{
	Class.forName(driver).newInstance();
	
	connection = DriverManager.getConnection(url, username, password);
	
	statement = connection.createStatement();
	rs = statement.executeQuery("SELECT * FROM user_log_in where username = '"+user_name+"'");
	
	if(rs.next())
	{
		String s = rs.getString("password");
		String id = rs.getString("id");
		String type = rs.getString("type");
		String status = rs.getString("status");
		String ref_id = rs.getString("ref_id");
		if(s.equals(passwd) && status.equals("accepted"))
		{
			events[0]="username: (" + user_name + ") sign in";
			events[1] = "Login successful";
			session.setAttribute("session_name",user_name);
			session.setAttribute("id",id);
			session.setAttribute("type",type);
			session.setAttribute("ref_id",ref_id);
			
		}	
		else
		{
			events[0]="username: (" + user_name + ") sign in";
			events[1] = "Login failed. Invalid password";
		}
			
	}
	else
	{
		events[0]="Unknown username: (" + user_name + ") Tring to sign in";
		events[1] = "Login failed. Invalid username";
	}
	response.sendRedirect("index.jsp");
	rs2 = statement.executeQuery("SELECT COUNT(*) FROM events");
	rs2.next();
	int counter  = rs2.getInt(1);
//--------------------------------------------------------------
//----------- add events----------------------------------------
//--------------------------------------------------------------
	
	if(counter > 0)
	{
		rs3=statement.executeQuery("SELECT MAX(no) FROM events");
		rs3.next();
		int max = rs3.getInt(1);
		counter = max+1;
	}
	else
		counter = 1;
	
	String query= "INSERT INTO events values ("+counter + ",'"+events[0]+"','"+events[1]+"','"+events[2]+"','"+events[3]+"','login')";
	statement.executeUpdate(query);
	
//--------------------------------------------------------------
	rs.close();

		connection.close();
		
	}catch(ClassNotFoundException cnfe)
	{
		out.println("<br>Couldn't find class file" + cnfe); 

	}catch(SQLException sqle)
	{
		out.println("<br>SQL Exception: " + sqle);
	}catch(InstantiationException ie)
	{
		out.println("<br>Instant exception : " + ie);
	}catch(IllegalAccessException iae)
	{
		out.println("<br>Illegal access exception : " + iae);
	}
} else {
	%>
	<jsp:include page="login.html"/>
	<%
}
%>